#include "capture_timer1.h"
#include "ddl.h"
#include "bt.h"
#include "flash.h"
#include "gpio.h"

#include "spwm_int.h"

volatile uint32_t u32PwcCapValue;
volatile uint16_t u16TIM1_CntValue;

/*
int32_t main(void)
{
    App_ClockCfg();         //时钟配置初始化
    
    App_Timer1PortCfg();    //端口配置初始化
    
    App_Timer1Cfg();        //Timer0配置初始化

    Bt_M1_Run(TIM1);        //TIM0 运行
    
    while (1);
}
*/

//端口配置初始化
void App_Timer1PortCfg(void)
{
    stc_gpio_cfg_t          stcTIM1Port;
    
    //结构体初始化清零
    DDL_ZERO_STRUCT(stcTIM1Port);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); //GPIO 外设时钟使能
    
    //PA02设置为TIM1_CHA
    stcTIM1Port.enDir  = GpioDirIn;
    Gpio_Init(GpioPortA, GpioPin2, &stcTIM1Port);
    Gpio_SetAfMode(GpioPortA,GpioPin2,GpioAf4);
    
}

//Timer1 配置
void App_Timer1Cfg(void)
{
    uint16_t                u16CntValue;
    stc_bt_mode1_cfg_t      stcBtBaseCfg;
    stc_bt_pwc_input_cfg_t  stcBtPwcInCfg;

    //结构体初始化清零
    DDL_ZERO_STRUCT(stcBtBaseCfg);
    DDL_ZERO_STRUCT(stcBtPwcInCfg);

    //Sysctrl_TimerPllClkSel(SysctrlTimerClkPll2);               //定时器时钟倍频
    Sysctrl_SetPeripheralGate(SysctrlPeripheralBaseTim, TRUE); //Base Timer外设时钟使能
    
    stcBtBaseCfg.enWorkMode = BtWorkMode1;                  //定时器模式
    stcBtBaseCfg.enCT       = BtTimer;                      //定时器功能，计数时钟为内部PCLK
    stcBtBaseCfg.enPRS      = BtPCLKDiv32;                  //PCLK/32
    stcBtBaseCfg.enOneShot  = BtPwcCycleDetect;             //PWC循环检测
    Bt_Mode1_Init(TIM1, &stcBtBaseCfg);                     //TIM1 的模式1功能初始化
    
    stcBtPwcInCfg.enTsSel  = BtTs6IAFP;                     //PWC输入选择 CHA
    stcBtPwcInCfg.enIA0Sel = BtIA0Input;                    //CHA选择IA0
    stcBtPwcInCfg.enFltIA0 = BtFltPCLKDiv16Cnt3;            //PCLK/16 3个连续有效
    //stcBtPwcInCfg.enIB0Sel = BtIB0Input;                  //CHB选择IB0
    //stcBtPwcInCfg.enFltIB0 = BtFltPCLKDiv16Cnt3;          //PCLK/16 3个连续有效
    Bt_M1_Input_Cfg(TIM1, &stcBtPwcInCfg);                  //PWC输入设置
    
    Bt_M1_PWC_Edge_Sel(TIM1, BtPwcRiseToRise);              //上升沿到上升沿捕获  BtPwcFallToRise
    
    u16CntValue = 0;
    Bt_M1_Cnt16Set(TIM1, u16CntValue);                      //设置计数初值  
    
    Bt_ClearAllIntFlag(TIM1);                               //清中断标志
    Bt_Mode1_EnableIrq(TIM1, BtUevIrq);                     //使能TIM1溢出中断
    Bt_Mode1_EnableIrq(TIM1, BtCA0Irq);                     //使能TIM1捕获中断
    EnableNvic(TIM1_IRQn, IrqLevel3, TRUE);                 //TIM1中断使能
}


/*******************************************************************************
 * TIM1中断服务函数
 ******************************************************************************/
unsigned short capture_count=0;
unsigned short captureArr[512] = {0};
unsigned short captureValue=0;
unsigned char captureFlag=0;

#if 0 // 原计算上升沿脉宽，但占空比不是正好百分之五十。
void Tim1_IRQHandler(void)
{
    static uint16_t u16TIM1_OverFlowCnt;
    
    static uint16_t u16TIM1_CapValue;
     
    //Timer0 模式1 计数溢出中断
    if(TRUE == Bt_GetIntFlag(TIM1, BtUevIrq))
    {
        u16TIM1_OverFlowCnt++;        //计数脉宽测量开始边沿到结束边沿过程中timer的溢出次数
        
        Bt_ClearIntFlag(TIM1,BtUevIrq); //清除中断标志
    }

    //Timer0 模式1 PWC脉宽测量中断
    if(TRUE == `Bt_GetIntFlag(TIM1, BtCA0Irq))
    {
        u16TIM1_CntValue = Bt_M1_Cnt16Get(TIM1);         //读取当前计数值
        u16TIM1_CapValue = Bt_M1_PWC_CapValueGet(TIM1);  //读取脉宽测量值
#if 1
        u32PwcCapValue = u16TIM1_OverFlowCnt*0x10000 + u16TIM1_CapValue;
        //if(capture_count>=512)
        //    capture_count = 0;
        //captureArr[capture_count++] = (unsigned short)u32PwcCapValue;
        //if(u32PwcCapValue>9600)
        //    u32PwcCapValue = 0;

      captureValue = (unsigned short)u32PwcCapValue;
     // if(SPWMCnt<120||SPWMCnt>136)
     //   SPWMCnt = 0;
#endif  
        u16TIM1_OverFlowCnt = 0;
        
        captureFlag = 1;
        Bt_ClearIntFlag(TIM1, BtCA0Irq); //清除中断标志
    }
}
#endif


// 计算两个上升沿的宽度来确定频率
void Tim1_IRQHandler(void)
{ 
    static uint16_t u16TIM1_OverFlowCnt;
    
    static uint16_t u16TIM1_CapValue;
     
    //Timer0 模式1 计数溢出中断
    if(TRUE == Bt_GetIntFlag(TIM1, BtUevIrq))
    {
        u16TIM1_OverFlowCnt++;        //计数脉宽测量开始边沿到结束边沿过程中timer的溢出次数
        
        Bt_ClearIntFlag(TIM1,BtUevIrq); //清除中断标志
    }

    //Timer0 模式1 PWC脉宽测量中断
		
    if(TRUE == Bt_GetIntFlag(TIM1, BtCA0Irq))
    {
        u16TIM1_CntValue = Bt_M1_Cnt16Get(TIM1);         //读取当前计数值
        u16TIM1_CapValue = Bt_M1_PWC_CapValueGet(TIM1);  //读取脉宽测量值

        u32PwcCapValue = u16TIM1_OverFlowCnt*0x10000 + u16TIM1_CapValue;

		    captureValue = (unsigned short)u32PwcCapValue;

        u16TIM1_OverFlowCnt = 0;
        
        captureFlag = 1;
		
        Bt_ClearIntFlag(TIM1, BtCA0Irq); //清除中断标志
    }
		
		if(((captureValue/10000)%10)!=0)//去除超频信号
		{ 
		  if(SPWMCnt>100||SPWMCnt<20)//防止在下降沿误中断重新生成sin波
			{
		     SPWMCnt=128;
			}
		}
		
}